﻿using OpenBlock.Properties;
using OpenBlock.Terrain;
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

namespace OpenBlock.IO
{
    public class LevelLoader
    {
        public static readonly string REGIONS_DIR_NAME = "regions";
        
        public string levelName;
        public int type;

        private string levelPath;

        public string RegionsPath => levelPath + Path.DirectorySeparatorChar + REGIONS_DIR_NAME;

        private LevelRegion origin;

        public LevelLoader()
        {

        }

        public void Load()
        {
            
        }

        public Task<Chunk> LoadChunk(Vector3Int chunkPos)
        {
            return null;
        }

        public void SaveChunk(Chunk chunk)
        {

        }

        public bool HasChunk(Vector3Int chunkPos)
        {
            var regionPos = ChunkPos2RegionPos(chunkPos);
            return regionPos == Vector3Int.zero;
        }

        public void Close()
        {
            
        }

        private string GetRegionPath(Vector3Int regionPos)
        {
            return RegionsPath + Path.DirectorySeparatorChar + $"m.{regionPos.x}.{regionPos.y}.{regionPos.z}.obr";
        }

        private static Vector3Int ChunkPos2RegionPos(Vector3Int chunkPos)
        {
            return new Vector3Int(Mathf.FloorToInt(chunkPos.x / 2.0f), Mathf.FloorToInt(chunkPos.y / 4.0f), Mathf.FloorToInt(chunkPos.z / 2.0f));
        }
    }
}
